package com.igs.backup;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;

import com.igs.parse.quartz.FinalStringUtil;

public class DBBackupUtil
{
	private static Logger log = Logger.getLogger(DBBackupUtil.class);
	public static void backup()
	{
		//mysqldump -uroot -p123456 igs > D:\\temp\\igs_001.sql
		try
		{
			/** 输出格式: 2006-01-01 01:00:00 */
	        String dateStr=(new java.text.SimpleDateFormat("yyyyMMddhhmmss")).format(new Date());
			String sqlFilePath = FinalStringUtil.downLoadBackupPath + "\\igsdb" + dateStr + ".sql";
			String command="mysqldump -uroot -p123456 igs > "+sqlFilePath;
			Runtime run = Runtime.getRuntime();
			Process p = run.exec("cmd /c "+command);
			
		} catch (Exception ex)
		{
			log.error("备份数据库异常", ex);
		}
	}
	
	public static String getLastDownLoadFile()
	{
		File folder = new File(FinalStringUtil.downLoadBackupPath);
		if(folder.exists()==false)
			return null;
		File[] files = folder.listFiles();
		Long lastTime=new Long(0);;
		File lastFile=null;
		for (File f : files)
		{
			String fileName=f.getName();
			if(f.isFile() && fileName.endsWith(".sql"))
			{
				Long fileTime =f.lastModified();
				if(fileTime>lastTime)
				{
					lastTime=fileTime;
					lastFile=f;
				}
			}
			else
			{
				continue;
			}
		}
		if(lastFile!=null)
		{
			return FinalStringUtil.downLoadBackupPath+lastFile.getName();
		}
		return null;
	}
	
	
}
